import React, { Component } from 'react'
import ReactDOM from 'react-dom';
import { Modal, Form } from 'antd';
import { ModalForm } from '../../components'
import { fetchData } from '../../components/AjaxConsumer'
import store from '../../stores'
const confirm = Modal.confirm;

const WrappedAdvancedSearchForm = Form.create()(ModalForm);

export default class UpdateWaverulePage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            visible: false,
            resData: null,
            submitUrl:'/base/data/waverule/c'
        };
        //和stroes进行通信挂钩 并绑定解绑事件unsubscribe
        this.unsubscribe = store.listen((storeResData)=>{
            this.setState({
                storeResData: storeResData
            })
        })

        this.formAlerSize = 'large'; //弹出框的大小 只有'large'和'small'两种值 （必须,输错时默认为large）
        this.getFormData = [
            {
                type : 'hiddenInput',
                showName : 'waveRuleId',
                name : 'waveRuleId',
                initialValue : ''
            },
            {
                type : 'input',
                showName : '波次规则编号',
                name : 'waveRuleNo',
                initialValue : '',
                required:'required'  //选填
            },{
                type : 'input',
                showName : '波次规则描述',
                name : 'waveRuleDesc',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '仓库代码',
                name : 'whCode',
                initialValue : '',
                required:'required'
            },{
                type : 'select',
                showName : '区域',
                name : 'regionCode',
                optionList:[              //必填  下拉框的信息
                ],
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '库区编码',
                name : 'areaCode',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '路线',
                name : 'route',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '送达方编码',
                name : 'sdfCd',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '承运人',
                name : 'carrier',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '订单类型',
                name : 'orderType',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '客户',
                name : 'custCode',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '商家',
                name : 'bussinesType',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '运输方式',
                name : 'tranType',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '按配车单分组',
                name : 'vehicleGroup',
                initialValue : '',
                required:'required'
            },{
                type : 'select',
                showName : '单品',
                name : 'sku',
                initialValue : '',
                required:'required',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            },{
                type : 'input',
                showName : '订单数量上限',
                name : 'orderCount',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '订单数量下限',
                name : 'orderCountLow',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '固定收货人数量',
                name : 'recevierCount',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '体积限定',
                name : 'volumeLimit',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '重量限定',
                name : 'weightLimit',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '最长等待时间分',
                name : 'waitMin',
                initialValue : '',
                required:'required'
            },{
                type : 'select',
                showName : '优先级',
                name : 'priority',
                initialValue : '',
                required:'required',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'0'
                    },{
                        optionKey:'1',
                        optionShow:'1'
                    },{
                        optionKey:'2',
                        optionShow:'2'
                    },{
                        optionKey:'3',
                        optionShow:'3'
                    },{
                        optionKey:'4',
                        optionShow:'4'
                    },{
                        optionKey:'5',
                        optionShow:'5'
                    },{
                        optionKey:'6',
                        optionShow:'6'
                    },{
                        optionKey:'7',
                        optionShow:'7'
                    },{
                        optionKey:'8',
                        optionShow:'8'
                    },{
                        optionKey:'9',
                        optionShow:'9'
                    },{
                        optionKey:'10',
                        optionShow:'10'
                    }
                ]
            },{
                type : 'input',
                showName : '分配规则',
                name : 'shelvesRule',
                initialValue : '',
                required:'required'
            },{
                type : 'select',
                showName : '集货位推荐类型',
                name : 'gatherLocType',
                initialValue : '',
                required:'required',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'网格组'
                    },{
                        optionKey:'1',
                        optionShow:'车队'
                    },{
                        optionKey:'2',
                        optionShow:'送达方'
                    }
                ]
            },{
                type : 'input',
                showName : '直接分配库存',
                name : 'directDistRepertory',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '订单时间',
                name : 'orderTime',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '应发货时间窗口',
                name : 'orderSendTime',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '固定产品数量',
                name : 'fixProductCount',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '最小提总数',
                name : 'minCount',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '总件数设定',
                name : 'allCountLimit',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '波次数',
                name : 'waveCount',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '整箱约束',
                name : 'completeBox',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '是否面签拣货',
                name : 'visaPick',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '扩展SQL',
                name : 'extenSql',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '执行间隔',
                name : 'intervals',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '执行开始时间',
                name : 'carriedOutStart',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '执行结束时间',
                name : 'carriedOutEnd',
                initialValue : '',
                required:'required'
            },{
                type : 'dataTimePicker',
                showName : '最近执行时间',
                name : 'recentExecuteTime',
                initialValue : '',
                required:'required'
            },{
                type : 'dataTimePicker',
                showName : '下次执行时间',
                name : 'nextTime',
                initialValue : '',
                required:'required'
            }

        ];
    }
    //组件销毁时解绑与stores的监听
    componentWillUnmount(){
        this.unsubscribe();
    }
    //显示弹框
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    // 隐藏弹出框
    hideModal = () => {
        this.setState({
            visible: false,
        });
    }
    // 新增库区
    addWaveruleHandle = () => {
        this.setState({
            submitUrl:'/base/data/waverule/c',
            formTitle: '新增波次规则',
            resData: null
        });
        this.showModal();//显示弹框
    }

    // 新增库区
    addInitLocationHandle = () => {
        this.setState({resData: null});
        //根据对象的Id 属性获取对象信息
        const options= {
            url: '/base/region/all/',
            type: 'get',//请求方式
            condition:{}
        };
        fetchData(this, options, this.addLocationHandle);
    }


    // 获取编辑信息后的回调
    editGetInfoCallBack11 = (resData) =>{
        let tmpFormData = this.state.formData;
        // 编辑时候渲染初始数据
        // 1 先加载下拉框
        let factoryCode = 'all';
        let options= {
            url: '/base/data/warehouse/r/'+factoryCode+'/warehouse',
            type: 'post',
            condition:{}
        };
        // 当前item塞入resData
        this.setState({resData:resData});
        fetchData(this, options, this.queryWarehousesUnderFactoryEditCallBackHandle);

    }
    // 编辑时候获取下拉框
    queryWarehousesUnderFactoryEditCallBackHandle=(listData)=>{
        let tmpFormData = this.state.formData;
        // 首先将仓库下拉框填充
        let optionList = [];
        optionList.push({optionKey:'',optionShow:'请选择'});
        listData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.warehouseCode;
            tmp.optionShow = e.warehouseCode;
            optionList.push(tmp);
        });
        tmpFormData[3].optionList = optionList;
        this.setState(
            {
                formData:tmpFormData
            }
        );
        // 其次填充区域下拉框

        // 动态加载区域
        let whCode = this.state.resData.data.whCode;
        let areaOptions= {
            url: '/base/region/r/'+whCode+'/region',
            type: 'post',
            condition:{}
        };
        console.log('编辑时动态加载库区下的区域',areaOptions);
        fetchData(this, areaOptions, this.queryRegionsUnderWareHouseEditCallBackHandle);

    }
    // 填充编辑时仓库和区域下拉框
    queryRegionsUnderWareHouseEditCallBackHandle =(listData) =>{
        console.log('--------------------',listData);
        let optionList = [];
        optionList.push({optionKey:'',optionShow:'请选择'});
        let tmpFormData = this.state.formData;
        if(listData.data.length>0){

            listData.data.map( (e,index) => {
                let tmp = {};
                tmp.optionKey = e.regionCode;
                tmp.optionShow = e.regionCode;
                optionList.push(tmp);
            });

        }

        tmpFormData[4].optionList = optionList;
        // this.setState({
        //         formData:tmpFormData});
        console.log("-------------------");
        let resData = this.state.resData;
        if(resData != null && resData.data != null){
            let itemData = resData.data;
            tmpFormData.map( (e, index) => {
                // 其次告诉仓库下拉框现在选中哪个
                if(e.type=='select'){
                    if(e.name=='whCode'){
                        tmpFormData[3].initialValue=itemData[e.name];
                    }else if(e.name=='regionCode'){
                        tmpFormData[4].initialValue=itemData[e.name];
                    }
                }else{
                    e.initialValue = itemData[e.name]
                }
            });
        }
        // 更新state
        this.setState({
            //submitUrl:'/base/data/area/u',
            submitUrl:'/base/data/waverule/u',
            resData: resData,
            visible:true,
            formData:tmpFormData
        });
    }



    editGetInfoCallBack = (resData) =>{
        this.setState({
            submitUrl:'/base/data/waverule/u',
            resData: resData
        });
        this.showModal();//显示弹框
    }

    // 编辑信息
    editHandle = (rowObj) => {
        this.setState({resData: null});
        //根据对象的Id 属性获取对象信息
        let waveRuleId = rowObj.waveRuleId;
        const options= {
            url: '/base/data/waverule/r/'+waveRuleId,
            type: 'get',//请求方式
            condition:{}
        };
        fetchData(this, options, this.editGetInfoCallBack);
    }


    // 删除后的回调
    deleteCallBackHandle = ()=>{
        let result = this.state.resData;
        if (result.code == '0'){
            alert(result.msg);
        }else {
            this.refreshHandle();
        }
    }

    // 删除用户
    deleteHandle = (rowObj) => {
        let waveRuleId = rowObj.waveRuleId;

        const options= {
            url: '/base/data/waverule/d',
            condition:{waveRuleId:waveRuleId}
            /*contentType:'application/x-www-form-urlencoded'*/
        };
        let _that = this;
        confirm({
            title: '您确定要删除吗？',
            onOk() {
                fetchData(_that, options, (resData) => {
                    _that.setState({
                        resData: resData
                    });
                    if (resData.code == '0'){
                        alert(resData.msg);
                    }else {
                        _that.refreshHandle();
                    }
                });
            },
            onCancel() {
                console.log("取消了删除操作")
            },
        });
    }


    // 新增/修改后的回调
    handleOk = () => {
        // let result = this.state.resData;
        this.hideModal();
        this.refreshHandle();
    }

    handleCancel = () => {
        this.hideModal();
    }

    //刷新List列表
    refreshHandle = () => {
        console.log("刷新List列表")
        this.props.refreshHandle();
    }

    componentDidMount() {
        const options= {
            url: '/base/region/all',
            type: 'get',
            condition:{}
        };
        fetchData(this, options, this.dicInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        //alert('resData' + JSON.stringify(resData, null, 3));
        let optionList = [];
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.regionCode;
            tmp.optionShow = e.regionDesc;
            optionList.push(tmp);
        });

        this.getFormData[3].optionList = optionList;
    }

    render() {

        if(this.state.resData != null &&  this.state.resData.data != null){
            let dock = this.state.resData.data;
            this.getFormData.map( (e, index) => {
                e.initialValue = dock[e.name]
            });
        } else {
            this.getFormData.map( (e, index) => {
                if(e.type == 'select'){
                    e.initialValue = '1'
                } else if (e.type == 'datapacker') {
                    e.initialValue = ''
                }
            });
        }

        return(
            <Modal
                style={{top:'20px'}}
                visible={this.state.visible}
                title={this.state.formTitle ? this.state.formTitle : '填写表单'}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                width={this.formAlerSize=='large'?'850px':'400px'}
                footer=''
            >

                <WrappedAdvancedSearchForm
                    ref="modalFrom"
                    title="波次规则信息"
                    formAlerSize={this.formAlerSize}
                    formUrl={this.state.submitUrl}
                    getFormData={this.getFormData}
                    closeBox={this.handleCancel}
                    checkBox={this.handleOk} />

            </Modal>
        )
    }
}
